// import 'package:flutter/foundation.dart' show kIsWeb;

import 'package:dio/dio.dart';
import 'package:flutter/widgets.dart';

import 'package:flutter_stock_analyze/env/env.dart';

class Http {
  Http([this.context]) {
    _options = Options(extra: {"context": context, "noCache": true});
    print("Http init:$host");
  }

  BuildContext? context;
  Options? _options;

  static Dio dio = Dio();
  // final String host = "http://localhost:5000";
  // final String host = "https://stock.myfile.live";
  static final String host = Env.envConfig.baseUrl;

  void initDio() {
    dio.interceptors.add(
      InterceptorsWrapper(
        onRequest: (options, handler) => {
          // if (!kIsWeb) {options.path = '$host${options.path}'},
          options.path = '$host${options.path}',
        },
      ),
    );
  }

  Future<String> fetchAnalysis(String stockCode, period, adjust) async {
    final response = await dio.post(
      '/analyze/$stockCode',
      data: {'period': period, 'adjust': adjust},
      options: _options,
    );
    print(response);
    return response.data;
  }
}
